﻿using System;
using System.Drawing;
using Felbot.IO;
using Felbot.Profiles;

namespace Felbot.Interface.Conversations {
    /// <summary>
    /// Allows Font, Color, and Style control.
    /// </summary>
    public static class FCSControl {
        /// <summary>Gets the default setting for an option.</summary>
        /// <param name="key">The option key.</param>
        /// <returns>The default setting.</returns>
        public static FCSSetting GetDefaultSetting(FCSObjects key) {
            switch (key) {
                case FCSObjects.Chatroom_Text: return new FCSSetting(null, FontStyle.Regular, Color.White, Color.Black, FCSStyle.Default, "%t");
                case FCSObjects.Chatroom_Timestamp: return new FCSSetting(null, FontStyle.Regular, Color.White, Color.Black, FCSStyle.Default, "[%t] ");
                case FCSObjects.Chatroom_Bot_Try: return new FCSSetting(null, FontStyle.Regular, Color.Yellow, Color.Black, FCSStyle.Default, "%t");
                case FCSObjects.Chatroom_Bot_Success: return new FCSSetting(null, FontStyle.Regular, Color.GreenYellow, Color.Black, FCSStyle.Default, "%t");
                case FCSObjects.Chatroom_Bot_Fail: return new FCSSetting(null, FontStyle.Regular, Color.Red, Color.Black, FCSStyle.Default, "%t");
                case FCSObjects.Chatroom_Information_Info: return new FCSSetting(null, FontStyle.Regular, Color.DeepSkyBlue, Color.Black, FCSStyle.Default, "%t");
                case FCSObjects.Chatroom_Information_Error: return new FCSSetting(null, FontStyle.Regular, Color.Red, Color.Black, FCSStyle.Default, "%t");
                case FCSObjects.Chatroom_Information_Username: return new FCSSetting(null, FontStyle.Regular, Color.Yellow, Color.Black, FCSStyle.Default, "%t");
                case FCSObjects.Chatroom_Information_Status: return new FCSSetting(null, FontStyle.Regular, Color.White, Color.Black, FCSStyle.Default, "%t");
                case FCSObjects.Chatroom_Information_Clan: return new FCSSetting(null, FontStyle.Regular, Color.Cyan, Color.Black, FCSStyle.Default, "%t");
                case FCSObjects.Chatroom_Information_Command: return new FCSSetting(null, FontStyle.Regular, Color.SeaGreen, Color.Black, FCSStyle.Default, "%t");
                case FCSObjects.Chatroom_Talk_NormalText: return new FCSSetting(null, FontStyle.Regular, Color.White, Color.Black, FCSStyle.Default, "%t");
                case FCSObjects.Chatroom_Talk_EmotedText: return new FCSSetting(null, FontStyle.Regular, Color.LightGray, Color.Black, FCSStyle.Default, "%t");
                case FCSObjects.Chatroom_Talk_WhisperedFromText: return new FCSSetting(null, FontStyle.Regular, Color.Gray, Color.Black, FCSStyle.Default, "%t");
                case FCSObjects.Chatroom_Talk_WhisperedToText: return new FCSSetting(null, FontStyle.Regular, Color.Gray, Color.Black, FCSStyle.Default, "%t");
                case FCSObjects.Chatroom_Talk_UserName: return new FCSSetting(null, FontStyle.Regular, Color.Yellow, Color.Black, FCSStyle.Default, "<%t> ");
                case FCSObjects.Chatroom_Talk_OperatorName: return new FCSSetting(null, FontStyle.Regular, Color.Gold, Color.Black, FCSStyle.Default, "<%t> ");
                case FCSObjects.Chatroom_Talk_RepresentativeName: return new FCSSetting(null, FontStyle.Regular, Color.RoyalBlue, Color.Black, FCSStyle.Default, "<%t> ");
                case FCSObjects.Chatroom_Talk_EmotedName: return new FCSSetting(null, FontStyle.Regular, Color.Silver, Color.Black, FCSStyle.Default, "%t ");
                case FCSObjects.Chatroom_Talk_WhisperedFromName: return new FCSSetting(null, FontStyle.Regular, Color.Silver, Color.Black, FCSStyle.Default, "%t whispers: ");
                case FCSObjects.Chatroom_Talk_WhisperedToName: return new FCSSetting(null, FontStyle.Regular, Color.Silver, Color.Black, FCSStyle.Default, "You whisper to %t: ");
                case FCSObjects.Chatroom_Talk_InvisibleName: return new FCSSetting(null, FontStyle.Regular, Color.LightGray, Color.Black, FCSStyle.Default, "<%t (Invisible)> ");
                case FCSObjects.Chatroom_Talk_YourName: return new FCSSetting(null, FontStyle.Regular, Color.LightBlue, Color.Black, FCSStyle.Default, "<%t> ");
                case FCSObjects.Chatroom_Talk_WatchedName: return new FCSSetting(null, FontStyle.Bold, Color.Empty, Color.Black, FCSStyle.Default, "<%t> ");
                case FCSObjects.Userlists_Header: return new FCSSetting(null, FontStyle.Bold, Color.White, Color.Blue, FCSStyle.Default, " - %t - ");
                case FCSObjects.Userlists_Item: return new FCSSetting(null, FontStyle.Regular, Color.GreenYellow, Color.Black, FCSStyle.Default, "%t");
                case FCSObjects.Userlists_Channel_Normal: return new FCSSetting(null, FontStyle.Regular, Color.White, Color.Black, FCSStyle.Default, "%t");
                case FCSObjects.Userlists_Channel_Operator: return new FCSSetting(null, FontStyle.Regular, Color.Gold, Color.Black, FCSStyle.Default, "%t");
                case FCSObjects.Userlists_Channel_Representative: return new FCSSetting(null, FontStyle.Regular, Color.RoyalBlue, Color.Black, FCSStyle.Default, "%t");
                case FCSObjects.Userlists_Channel_Invisible: return new FCSSetting(null, FontStyle.Regular, Color.Gray, Color.Black, FCSStyle.Default, "%t");
                case FCSObjects.Userlists_Channel_Squelched: return new FCSSetting(null, FontStyle.Regular, Color.DarkRed, Color.Black, FCSStyle.Default, "%t");
                case FCSObjects.Userlists_Channel_You: return new FCSSetting(null, FontStyle.Bold, Color.Empty, Color.Black, FCSStyle.Default, "%t");
                case FCSObjects.Userlists_Channel_Watched: return new FCSSetting(null, FontStyle.Underline, Color.Empty, Color.Black, FCSStyle.Default, "%t");
                case FCSObjects.Userlists_Channel_Idle: return new FCSSetting(null, FontStyle.Italic, Color.Empty, Color.Black, FCSStyle.Default, "%t");
                case FCSObjects.Userlists_Friend_Online: return new FCSSetting(null, FontStyle.Regular, Color.White, Color.Black, FCSStyle.Default, "%t");
                case FCSObjects.Userlists_Friend_Offline: return new FCSSetting(null, FontStyle.Regular, Color.Gray, Color.Black, FCSStyle.Default, "%t");
                case FCSObjects.Userlists_Friend_Mutual: return new FCSSetting(null, FontStyle.Bold, Color.Empty, Color.Black, FCSStyle.Default, "%t");
                case FCSObjects.Userlists_Clan_Online: return new FCSSetting(null, FontStyle.Regular, Color.White, Color.Black, FCSStyle.Default, "%t");
                case FCSObjects.Userlists_Clan_Offline: return new FCSSetting(null, FontStyle.Regular, Color.Gray, Color.Black, FCSStyle.Default, "%t");
                case FCSObjects.Userlists_Clan_You: return new FCSSetting(null, FontStyle.Regular, Color.Empty, Color.Black, FCSStyle.Default, "%t");
                default: return null;
            }
        }

        /// <summary>Gets the default setting for an option.</summary>
        /// <param name="key">The option key.</param>
        /// <returns>The default setting.</returns>
        public static FCSSetting GetDefaultSetting(string key) {
            return GetDefaultSetting(FCSObjectFromString(key));
        }

        /// <summary>Gets the current setting for an option.</summary>
        /// <param name="key">The option key.</param>
        /// <returns>The current setting.</returns>
        public static FCSSetting GetCurrentSetting(FCSObjects key) {
            return ProfileControl.Global.GetSetting("FontColorStyle", key.ToString()).ToFCSSetting(GetDefaultSetting(key));
        }

        /// <summary>Returns the enumerated value for a key.</summary>
        /// <param name="key">The option key.</param>
        /// <returns>The FCSObject enum value that corresponds with the string.</returns>
        private static FCSObjects FCSObjectFromString(string key) {
            foreach (FCSObjects fcsobject in Enum.GetValues(typeof(FCSObjects)))
                if (fcsobject.ToString() == key)
                    return fcsobject;
            return (FCSObjects) 0;
        }

        /// <summary>Allows for the RichTextBox to easily get the stylized text node for a font-color-style setting.</summary>
        /// <param name="key">The option key.</param>
        /// <param name="args">Arguments for the text setting.</param>
        /// <returns>The formatted TextNode.</returns>
        public static TextNode GetTextNode(FCSObjects key, string text) {
            return GetFromFCSSetting(
                FCSControl.GetCurrentSetting(key),
                FCSControl.GetDefaultSetting(key),
                text);
        }

        /// <summary></summary>
        /// <param name="a_fcsSetting"></param>
        /// <param name="a_fcsDefaultStyle"></param>
        /// <param name="a_dictVariables"></param>
        /// <returns></returns>
        public static TextNode GetFromFCSSetting(FCSSetting setting, FCSSetting defaultStyle, string text) {
            // get text and style
            string styleText;
            switch (setting.StyleType) {
                case FCSStyle.Default:
                default:
                    styleText = defaultStyle.StyleCustom;
                    break;
                case FCSStyle.Hidden:
                    return null;
                case FCSStyle.Custom:
                    styleText = setting.StyleCustom;
                    break;
            }

            // replace style with current vars
            text = styleText.Replace("%t", text);

            // font
            FontFamily family;
            Font font = null;
            if (setting.FontFamily != null) {
                family = new FontFamily(setting.FontFamily);
                font = new Font(family, 0);
            }
            if (setting.FontStyle != FontStyle.Regular)
                font = new Font(font, setting.FontStyle);

            // return
            if (font != null)
                return new TextNode(font, setting.ColorFore, text);
            else
                return new TextNode(setting.ColorFore, text);
        }
    }

    /// <summary>An enumeration of font-color-style settings available.</summary>
    public enum FCSObjects {
        /// <summary>Not a defined style.</summary>
        None,
        /// <summary>Any lines in the chat room.</summary>
        Chatroom_Text,
        /// <summary>Text saying something is being attempted.</summary>
        Chatroom_Bot_Try,
        /// <summary>Text saying something has succeeded.</summary>
        Chatroom_Bot_Success,
        /// <summary>Text saying something has failed or had an exception.</summary>
        Chatroom_Bot_Fail,
        /// <summary>Text saying Battle.net information.</summary>
        Chatroom_Information_Info,
        /// <summary>Text saying Battle.net error.</summary>
        Chatroom_Information_Error,
        /// <summary>Text saying the username of a user.</summary>
        Chatroom_Information_Username,
        /// <summary>Text saying the status of something online.</summary>
        Chatroom_Information_Status,
        /// <summary>Text saying something Battle.net Clan related.</summary>
        Chatroom_Information_Clan,
        /// <summary>Text saying the result of a command.</summary>
        Chatroom_Information_Command,
        /// <summary>Text saying the timestamp.</summary>
        Chatroom_Timestamp,
        /// <summary>Text showing a whisper from someone.</summary>
        Chatroom_Talk_WhisperedFromText,
        /// <summary>Text showing a whisper to someone.</summary>
        Chatroom_Talk_WhisperedToText,
        /// <summary>Text showing an emote.</summary>
        Chatroom_Talk_EmotedText,
        /// <summary>Text showing normal chat.</summary>
        Chatroom_Talk_NormalText,
        /// <summary>Text showing the talking user.</summary>
        Chatroom_Talk_UserName,
        /// <summary>Text showing the username of an emote from someone.</summary>
        Chatroom_Talk_EmotedName,
        /// <summary>Text showing the username of a whisper from someone.</summary>
        Chatroom_Talk_WhisperedFromName,
        /// <summary>Text showing the username of a whisper to someone.</summary>
        Chatroom_Talk_WhisperedToName,
        /// <summary>Text showing the talking user if an operator.</summary>
        Chatroom_Talk_OperatorName,
        /// <summary>Text showing the talking user if a Battle.net representative.</summary>
        Chatroom_Talk_RepresentativeName,
        /// <summary>Text showing an invisible user talking.</summary>
        Chatroom_Talk_InvisibleName,
        /// <summary>Text showing the bot user talking.</summary>
        Chatroom_Talk_YourName,
        /// <summary>Text showing a watched user talking.</summary>
        Chatroom_Talk_WatchedName,
        /// <summary>A userlist item showing an operator.</summary>
        Userlists_Channel_Operator,
        /// <summary>A userlist item showing a Battle.net representative.</summary>
        Userlists_Channel_Representative,
        /// <summary>A userlist item showing an invisible user.</summary>
        Userlists_Channel_Invisible,
        /// <summary>A userlist item showing a squelched user.</summary>
        Userlists_Channel_Squelched,
        /// <summary>A userlist item showing the bot user.</summary>
        Userlists_Channel_You,
        /// <summary>A userlist item showing a watched user.</summary>
        Userlists_Channel_Watched,
        /// <summary>A userlist item showing an idle user.</summary>
        Userlists_Channel_Idle,
        /// <summary>A userlist item showing an online friend.</summary>
        Userlists_Friend_Online,
        /// <summary>A userlist item showing an offline friend.</summary>
        Userlists_Friend_Offline,
        /// <summary>A userlist item showing a mutual friend.</summary>
        Userlists_Friend_Mutual,
        /// <summary>A userlist item showing an online clan member.</summary>
        Userlists_Clan_Online,
        /// <summary>A userlist item showing an offline clan member.</summary>
        Userlists_Clan_Offline,
        /// <summary>A userlist item showing the bot user in the clan.</summary>
        Userlists_Clan_You,
        /// <summary>The userlist header.</summary>
        Userlists_Header,
        /// <summary>The default item. The background color is also the drag-and-drop line style in the friend list.</summary>
        Userlists_Item,
        /// <summary>The default item in the channel list.</summary>
        Userlists_Channel_Normal
    }
}